package com.fortune.cat;

import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.ZkConnection;

import java.util.List;

public class ZkClientWatcherTwo {
 
	static final String CONNECT_ADDR = "192.168.2.101:2181";
	static final int SESSION_OUTTIME = 5000;// ms
	private static final String PARENT_PATH = "/testWatch";
	private static final String CHILDREN_PATH = "/testWatch/children";
 
	public static void main(String[] args) {
		ZkClient zkc = new ZkClient(new ZkConnection(CONNECT_ADDR), 5000);
 
		// 注册节点孩子信息变化
		zkc.subscribeChildChanges(PARENT_PATH, (parentPath, currentChilds) -> {
			System.out.println("subscribeChildChanges handleChildChange");
			System.out.println("parentPath: " + parentPath);
			System.out.println("currentChilds: " + currentChilds);
		});
		// 创建节点
		zkc.createPersistent(PARENT_PATH, "1234");
		// 创建子节点
		zkc.createPersistent(CHILDREN_PATH, "children");
		// 修改子节点的数据(不会触发subscribeChildChanges)
		zkc.writeData(CHILDREN_PATH, "hahaha");
 
		// 删除子节点
		zkc.deleteRecursive(CHILDREN_PATH);
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		// 删除节点
		zkc.deleteRecursive(PARENT_PATH);
		zkc.close();
	}
}